package com.xy.util;

import android.util.Log;

import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by XuYang
 * 2023/4/21
 * Email:544066591@qq.com
 */
public class TimeUtil {

    public static long getTimeMillis(String strTime) {
        long returnMillis = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = null;
        try {
            d = sdf.parse(strTime);
            returnMillis = d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return returnMillis;
    }

    public static String getTime(Date date) {//可根据需要自行截取数据显示
        Log.d("getTime()", "choice date millis: " + date.getTime());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static String getDate(Date date) {//可根据需要自行截取数据显示
        Log.d("getTime()", "choice date millis: " + date.getTime());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    //计算相差的小时
    public static String getTimeDifferenceHour(long starTime, long endTime) {
        String timeString = "";
        long diff = endTime - starTime;
        String string = Long.toString(diff);
        float parseFloat = Float.parseFloat(string);
        float hour1 = parseFloat / (60 * 60 * 1000);
        timeString = Float.toString(hour1);
        return timeString;
    }

    //获取网络时间
    public static long getNetworkTime(String address) {
        try {
            URL url = new URL(address);
            URLConnection uc = url.openConnection();
            uc.connect();
            return uc.getDate();
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}
